<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>Web server system</title>
</head>
<table class="fixed" border="5">
    <col width="1000px" /><col width="500px" />
    <tr><td>
        <h2 style=" text-align:center;"> **** Web Server ***</h2>
 
 
        <h3>wifi 密码配置</h3>
    <div>
        <label for="name">wifi名称</label>
        <input type="text" id="wifi" name="car_name" placeholder="ssid">
        <br>
        <label for="type">密码</label>
        <input type="text" id="code" name="car_type" placeholder="password">
        <br>
        <label for="type">服务器地址</label>
        <input type="text" id="ipv4" name="car_" placeholder="ipv4">
        <br>
        <label for="type">端口</label>
        <input type="text" id="port" name="car_" placeholder="port">
        <br>
        <button id ="send_WIFI" type="button" onclick="send_wifi()">提交</button>
    </div>
    </td><td>
        <table border="10">
            <tr>
                <td>
                    <label for="newfile">Upload a file</label>
                </td>
                <td colspan="2">
                    <input id="newfile" type="file" onchange="setpath()" style="width:100%;">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="filepath">Set path on server</label>
                </td>
                <td>
                    <input id="filepath" type="text" style="width:100%;">
                </td>
                <td>
                    <button id="upload" type="button" onclick="upload()">Upload</button>
                </td>
            </tr>
        </table>
    </td></tr>
</table>
<script>
function setpath() {
    var default_path = document.getElementById("newfile").files[0].name;
    document.getElementById("filepath").value = default_path;
}
function upload() {
    var filePath = document.getElementById("filepath").value;
    var upload_path = "/upload/" + filePath;
    var fileInput = document.getElementById("newfile").files;
 
    /* Max size of an individual file. Make sure this
     * value is same as that set in file_server.c */
    var MAX_FILE_SIZE = 200*1024;
    var MAX_FILE_SIZE_STR = "200KB";
 
    if (fileInput.length == 0) {
        alert("No file selected!");
    } else if (filePath.length == 0) {
        alert("File path on server is not set!");
    } else if (filePath.indexOf(' ') >= 0) {
        alert("File path on server cannot have spaces!");
    } else if (filePath[filePath.length-1] == '/') {
        alert("File name not specified after path!");
    } else if (fileInput[0].size > 200*1024) {
        alert("File size must be less than 200KB!");
    } else {
        document.getElementById("newfile").disabled = true;
        document.getElementById("filepath").disabled = true;
        document.getElementById("upload").disabled = true;
 
        var file = fileInput[0];
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (xhttp.readyState == 4) {
                if (xhttp.status == 200) {
                    document.open();
                    document.write(xhttp.responseText);
                    document.close();
                } else if (xhttp.status == 0) {
                    alert("Server closed the connection abruptly!");
                    location.reload()
                } else {
                    alert(xhttp.status + " Error!\n" + xhttp.responseText);
                    location.reload()
                }
            }
        };
        xhttp.open("POST", upload_path, true);
        xhttp.send(file);
    }
}

function convertObjectToQueryString(obj) {
    const parts = [];
    for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
            const value = obj[key];
            // 将值转换为字符串，以确保它可以被URL编码
            const keyValue = encodeURIComponent(value) + '';
            // 添加键值对到parts数组
            parts.push(`${key}=${keyValue}`);
        }
    }
    // 使用 '&' 连接所有的键值对
    return parts.join('&');
}

function send_wifi() {
    var input_ssid = document.getElementById("wifi").value;
    var input_code = document.getElementById("code").value;
    //
    var input_ipv4 = document.getElementById("ipv4").value;
    var input_port = document.getElementById("port").value;
    //
    var xhttp = new XMLHttpRequest();
        xhttp.open("POST", "/wifi_data", true);
        xhttp.onreadystatechange = function() {
            if (xhttp.readyState == 4) {
                if (xhttp.status == 200) {
                    console.log(xhttp.responseText);
                } else if (xhttp.status == 0) {
                    alert("Server closed the connection abruptly!");
                    location.reload()
                } else {
                    alert(xhttp.status + " Error!\n" + xhttp.responseText);
                    location.reload()
                }
            }
        };
    var data = {
        "ssid":input_ssid,
        "password":input_code,
        "ipv4":input_ipv4,
        "port":input_port
    }
    xhttp.send(convertObjectToQueryString(data));
}
 
</script>